package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.domain.*;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.*;
import com.itheima.service.MemberService;
import com.itheima.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private FoodMapper foodMapper;
    @Autowired
    private SportsMapper sportsMapper;
    @Autowired
    private InterventionMapper interventionMapper;

    //通过手机号查找会员
    public Member findByTelephone(String telephone) {
        return memberMapper.findByTelephone(telephone);
    }

    //添加会员
    public void add(Member member) {
        String password = member.getPassword();
        if (password != null){
            //使用MD5加密
            password = MD5Utils.md5(password);
            //加密后的密码赋值给会员
            member.setPassword(password);
        }
        Date date = new Date();
        member.setRegTime(date);
        memberMapper.add(member);
    }

    public List<Integer> findMemberCountByMonths(List<String> months) {
        List<Integer> list = new ArrayList<>();
        for (String month : months) {
            Integer count = memberMapper.findMemberCountBeforeDate(month);
            list.add(count);
        }
        return list;
    }
    //根据id查询所有
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //获取当前页码
        Integer currentPage = queryPageBean.getCurrentPage();
        //获取当前查询个数
        Integer pageSize = queryPageBean.getPageSize();
        //获取查询条件
        String queryString = queryPageBean.getQueryString();
        System.out.println(queryString);
        PageHelper.startPage(currentPage,pageSize);
        Page<Member> page= memberMapper.findPage(queryString);
         //获取查询结果
        List<Member> result = page.getResult();


        //数据库总个数
        long total = page.getTotal();
        System.out.println(total);
        //将获取到的数据封装到PageResult中
        return new PageResult(total,result);
    }

    @Override
    public Member findById(Integer id) {
        Member member = memberMapper.findById(id);
        return member;
    }
    @Override
    public void delete(Integer id) {
        Member member = memberMapper.findById(id);
        System.out.println(member);
        List<Order> orderList = member.getOrderList();
        System.out.println(orderList);
        if (orderList!=null&&orderList.size()>0){
            throw new RuntimeException("该数据有外键关联,请解除关联关系后重试");
        }
        memberMapper.deleteById(id);
    }

    @Override
    public Member findSingleMember(Integer id) {
        Member member= memberMapper.findSingleMember(id);
        return member;
    }

    @Override
    public List<Map> getsetmealListByPhoneNumber(String phoneNumber) {
        List<Map> list = memberMapper.getsetmealListByPhoneNumber(phoneNumber);
        for (Map map : list) {
            String orderStatus = (String) map.get("orderStatus");
            if ("已到诊".equals(orderStatus)){
                //获取订单id
                Integer id = (Integer) map.get("id");
                //根据订单id查询一个健康干预
                Intervention intervention = interventionMapper.findByOrderId(id);
                if (intervention!=null){
                    Integer interventionId = intervention.getId();
                    //根据健康干预id查询膳食建议
                    List<Food> foodList = foodMapper.findFoodsByInterventionId(interventionId);
                    intervention.setFoodList(foodList);
                    //根据健康干预id查询运动建议
                    List<Sports> sportsList = sportsMapper.findSportsByInterventionId(interventionId);
                    intervention.setSportsList(sportsList);
                    //添加到map中
                    map.put("doctorName",intervention.getDoctorName());
                    map.put("doctorAdvice",intervention.getDoctorAdvice());
                    StringBuilder foodAdvice = new StringBuilder();
                    if (foodList!=null&&foodList.size()>0){
                        for (Food food : foodList) {
                            foodAdvice.append(food.getName()).append(" ");
                        }
                    }
                    StringBuilder sportAdvice = new StringBuilder();
                    if (sportsList!=null&&sportsList.size()>0){
                        for (Sports sports : sportsList) {
                            sportAdvice.append(sports.getCampaignName()).append(" ").append(sports.getTheNumber()).append(" ").append(sports.getSportTime()).append("\n");
                        }
                    }
                    map.put("foods",foodAdvice.toString());
                    map.put("sports",sportAdvice.toString());
                }
            }
        }
        return list;
    }

    //修改会员信息
    public void update(Member member) {
        memberMapper.edit(member);
    }

    //通过会员id查询会员
    public Member findMemberById(String id) {
        Integer memberId = Integer.valueOf(id);
        return memberMapper.findById(memberId);
    }

   //修改会员手机号
    public void edit2Telephone(String id,String telephone) {
        Integer memberId = Integer.valueOf(id);
        memberMapper.edit2Telephone(memberId,telephone);
    }
}
